﻿using DAL.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL.YQZS
{
    public class TrackDAL
    {
        private readonly DataContext _dataContext;
        public TrackDAL(DataContext dataContext)
        {
            _dataContext = dataContext;
        }

        public async Task<IEnumerable<Track>> ListAsync(Guid id)
        {
            return await (from q in _dataContext.Tracks
                          where q.YqzsId == id
                          select q).ToListAsync();
        }

        public async Task<Track> GetAsync(Guid id)
        {
            return await _dataContext.Tracks.FindAsync(id);
        }

        public async Task CreateAsync(Track entity)
        {
            _dataContext.Entry(entity).State = EntityState.Added;
        }

        public async Task UpdateAsync(Track trackEntity)
        {
            _dataContext.Entry(trackEntity).State = EntityState.Modified;
        }

        public async Task DeleteAsync(IEnumerable<Track> deleteTrasks)
        {
            foreach (var item in deleteTrasks)
            {
                await DeleteAsync(item);
            }
        }

        public async Task DeleteAsync(Track entity)
        {
            _dataContext.Entry(entity).State = EntityState.Deleted;
        }

        public async Task DeleteAsync(Guid id)
        {
            var entity = await GetAsync(id);
            await DeleteAsync(entity);
        }

        public async Task SaveChangesAsync()
        {
            await _dataContext.SaveChangesAsync();
        }
    }
}
